home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / create_view.pro < prev    next >
Text File  |  1997-07-08  |  10KB  |  281 lines

  1. ; $Id: create_view.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1992-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6. ;+
  7. ; NAME:
  8. ;    CREATE_VIEW
  9. ;
  10. ; PURPOSE:
  11. ;    This procedure sets the various system variables required to
  12. ;       define a coordinate system and a 3-D view.   This procedure
  13. ;       builds the system viewing matrix (!P.T) in such a way that the
  14. ;       correct aspect ratio of the data is maintained even if the
  15. ;       display window is not square.
  16. ;       CREATE_VIEW also sets the "Data" to "Normal" coordinate
  17. ;       conversion factors (!X.S, !Y.S, and !Z.S) so that center of
  18. ;       the unit cube will be located at the center of the display
  19. ;       window.
  20. ;
  21. ; CATEGORY:
  22. ;    Viewing.
  23. ;
  24. ; CALLING SEQUENCE:
  25. ;       CREATE_VIEW
  26. ;    
  27. ; INPUTS:
  28. ;    None.
  29. ;
  30. ; KEYWORD PARAMETERS:
  31. ;       XMIN:       The minimum data value on the X axis.
  32. ;                   The default is (0.0).
  33. ;                   Data type : Any scalar numeric value.
  34. ;       XMAX:       The maximum data value on the X axis.
  35. ;                   The default is (1.0).
  36. ;                   Data type : Any scalar numeric value.
  37. ;       YMIN:       The minimum data value on the Y axis.
  38. ;                   The default is (0.0).
  39. ;                   Data type : Any scalar numeric value.
  40. ;       YMAX:       The maximum data value on the Y axis.
  41. ;                   Data type : Any scalar numeric value.
  42. ;                   The default is (1.0).
  43. ;       ZMIN:       The minimum data value on the Z axis.
  44. ;                   The default is (0.0).
  45. ;                   Data type : Any scalar numeric value.
  46. ;       ZMAX:       The maximum data value on the Z axis.
  47. ;                   The default is (1.0).
  48. ;                   Data type : Any scalar numeric value.
  49. ;       AX:         The orientation (X rotation) of the view.
  50. ;                   The default is (0.0).
  51. ;                   Data type : Float.
  52. ;       AY:         The orientation (Y rotation) of the view.
  53. ;                   The default is (0.0).
  54. ;                   Data type : Float.
  55. ;       AZ:         The orientation (Z rotation) of the view.
  56. ;                   The default is (0.0).
  57. ;                   Data type : Float.
  58. ;       WINX:       The X size, in pixels, of the window that the
  59. ;                   view is being set up for.
  60. ;                   The default is (640).
  61. ;                   Data type : Long.
  62. ;       WINY:       The Y size, in pixels, of the window that the
  63. ;                   view is being set up for.
  64. ;                   The default is (512).
  65. ;                   Data type : Long.
  66. ;       ZOOM:       The view zoom factor.   If zoom is a single
  67. ;                   value then the view will be zoomed equally in
  68. ;                   all 3 dimensions.   If zoom is a 3 element vector
  69. ;                   then the view will be scaled zoom(0) in X,
  70. ;                   zoom(1) in Y, and zoom(2) in Z.
  71. ;                   The default is (1.0).
  72. ;                   Data type : Float or Fltarr(3).
  73. ;       ZFAC:       Use this keyword to expand or contract the view
  74. ;                   in the Z dimension.
  75. ;                   The default is (1.0).
  76. ;                   Data type : Float.
  77. ;       PERSP:      The perspective projection distance.   A value of
  78. ;                   (0.0) indicates an isometric projection (NO per-
  79. ;                   spective).
  80. ;                   The default is (0.0).
  81. ;                   Data type : Float.
  82. ;       RADIANS:    Set this keyword to a non-zero value if the values
  83. ;                   passed to AX, AY, and AZ are in radians.
  84. ;                   The default is degrees.
  85. ;                   Data type : Int.
  86. ;
  87. ; SIDE EFFECTS:
  88. ;    This procedure sets the following IDL system variables :
  89. ;
  90. ;          !P.T, !P.T3D, !P.Position, !P.Clip, !P.Region
  91. ;          !X.S, !X.Style, !X.Range, !X.Margin
  92. ;          !Y.S, !Y.Style, !Y.Range, !Y.Margin
  93. ;          !Z.S, !Z.Style, !Z.Range, !Z.Margin
  94. ;
  95. ; PROCEDURE:
  96. ;       This procedure sets the 4x4 system viewing matrix (!P.T) by
  97. ;       calling T3D with the following parameters :
  98. ;
  99. ;       ; Reset (!P.T) to the identity matrix.
  100. ;          T3D, /RESET
  101. ;       ; Translate the center of the unit cube to the origin.
  102. ;          T3D, TRANSLATE=[(-0.5), (-0.5), (-0.5)]
  103. ;       ; Zoom the view.
  104. ;          T3D, SCALE=ZOOM
  105. ;       ; Scale the view to preserve the correct aspect ratio.
  106. ;          xrange = xmax - xmin
  107. ;          yrange = ymax - ymin
  108. ;          zrange = (zmax - zmin) * zfac
  109. ;          max_range = xrange > yrange > zrange
  110. ;          T3D, SCALE=([xrange, yrange, zrange] / max_range)
  111. ;       ; Rotate the view.
  112. ;          T3D, ROTATE=[0.0, 0.0, AZ]
  113. ;          T3D, ROTATE=[0.0, AY, 0.0]
  114. ;          T3D, ROTATE=[AX, 0.0, 0.0]
  115. ;       ; Define a perspective projection (if any).
  116. ;          IF (p_proj) THEN T3D, PERSPECTIVE=PERSP
  117. ;       ; Compensate for the aspect ratio of the display window.
  118. ;          T3D, SCALE=[xfac, yfac, 1.0]
  119. ;       ; Translate the unit cube back to its starting point.
  120. ;          T3D, TRANSLATE=[(0.5), (0.5), (0.5)]
  121. ;
  122. ; EXAMPLE:
  123. ;       Set up a view to display an iso-surface from volumetric data.
  124. ;
  125. ;       ; Create some data.
  126. ;          vol = FLTARR(40, 50, 30)
  127. ;          vol(3:36, 3:46, 3:26) = RANDOMU(s, 34, 44, 24)
  128. ;          FOR i=0, 10 DO vol = SMOOTH(vol, 3)
  129. ;
  130. ;       ; Generate the iso-surface.
  131. ;          SHADE_VOLUME, vol, 0.2, polygon_list, vertex_list, /LOW
  132. ;
  133. ;       ; Set up the view.
  134. ;       ; Note that the subscripts into the Vol array range from
  135. ;       ; 0 to 39 in X, 0 to 49 in Y, and 0 to 29 in Z.   As such,
  136. ;       ; the 3-D coordinates of the iso-surface (vertex_list) may
  137. ;       ; range from 0.0 to 39.0 in X, 0.0 to 49.0 in Y,
  138. ;       ; and 0.0 to 29.0 in Z.   Set XMIN, YMIN, and ZMIN to
  139. ;       ; zero (the default), and set XMAX=39, YMAX=49, and ZMAX=29.
  140. ;          WINDOW, XSIZE=600, YSIZE=400
  141. ;          CREATE_VIEW, XMAX=39, YMAX=49, ZMAX=29, AX=(-60.0), AZ=(30.0), $
  142. ;                       WINX=600, WINY=400, ZOOM=(0.7), PERSP=(1.0)
  143. ;
  144. ;       ; Display the iso surface in the specified view.
  145. ;          img = POLYSHADE(polygon_list, vertex_list, /DATA, /T3D)
  146. ;          TVSCL, img
  147. ;
  148. ; MODIFICATION HISTORY:
  149. ;     Written by:    Daniel Carr. Wed Sep  2 16:40:47 MDT 1992
  150. ;       Modified the way the view is compensated for the data aspect ratio.
  151. ;                       Daniel Carr. Tue Dec  8 17:53:54 MST 1992
  152. ;-
  153.  
  154. PRO Create_View, Xmin=p_xmin, Xmax=p_xmax, Ymin=p_ymin, Ymax=p_ymax, $
  155.                  Zmin=p_zmin, Zmax=p_zmax, Az=p_az, Ay=p_ay, Ax=p_ax, $
  156.                  Winx=p_winx, Winy=p_winy, Zoom=p_zoom, Zfac=p_zfac, $
  157.                  Persp=p_persp, Radians=p_radians
  158.  
  159. ; *** Test inputs
  160.  
  161. xmin = 0.0
  162. IF (N_Elements(p_xmin) GT 0) THEN xmin = Float(p_xmin[0])
  163. xmax = 1.0
  164. IF (N_Elements(p_xmax) GT 0) THEN xmax = Float(p_xmax[0])
  165. IF (xmax LE xmin) THEN BEGIN
  166.    Print, 'Xmax must be larger than Xmin'
  167.    STOP
  168. ENDIF
  169.  
  170. ymin = 0.0
  171. IF (N_Elements(p_ymin) GT 0) THEN ymin = Float(p_ymin[0])
  172. ymax = 1.0
  173. IF (N_Elements(p_ymax) GT 0) THEN ymax = Float(p_ymax[0])
  174. IF (ymax LE ymin) THEN BEGIN
  175.    Print, 'Ymax must be larger than Ymin'
  176.    STOP
  177. ENDIF
  178.  
  179. zmin = 0.0
  180. IF (N_Elements(p_zmin) GT 0) THEN zmin = Float(p_zmin[0])
  181. zmax = 1.0
  182. IF (N_Elements(p_zmax) GT 0) THEN zmax = Float(p_zmax[0])
  183. IF (zmax LE zmin) THEN BEGIN
  184.    Print, 'Zmax must be larger than Zmin'
  185.    STOP
  186. ENDIF
  187.  
  188. radians = 0B
  189. IF (N_Elements(p_radians) GT 0) THEN radians = Byte(p_radians[0])
  190.  
  191. az = 0.0
  192. IF (N_Elements(p_az) GT 0) THEN az = Float(p_az[0])
  193. IF (radians GE 1B) THEN az = az * 180.0 / !PI
  194. ay = 0.0
  195. IF (N_Elements(p_ay) GT 0) THEN ay = Float(p_ay[0])
  196. IF (radians GE 1B) THEN ay = ay * 180.0 / !PI
  197. ax = 0.0
  198. IF (N_Elements(p_ax) GT 0) THEN ax = Float(p_ax[0])
  199. IF (radians GE 1B) THEN ax = ax * 180.0 / !PI
  200.  
  201. winx = 640.0
  202. IF (N_Elements(p_winx) GT 0) THEN winx = Float(p_winx[0])
  203. IF (winx LT 2.0) THEN BEGIN
  204.    Print, 'Window X size must be >= 2'
  205.    STOP
  206. ENDIF
  207. winy = 512.0
  208. IF (N_Elements(p_winy) GT 0) THEN winy = Float(p_winy[0])
  209. IF (winy LT 2.0) THEN BEGIN
  210.    Print, 'Window Y size must be >= 2'
  211.    STOP
  212. ENDIF
  213.  
  214. zoom = [1.0, 1.0, 1.0]
  215. IF (N_Elements(p_zoom) GT 0) THEN zoom = $
  216.    [Float(p_zoom[0]), Float(p_zoom[0]), Float(p_zoom[0])]
  217. IF (N_Elements(p_zoom) GE 3) THEN zoom = $
  218.    [Float(p_zoom[0]), Float(p_zoom[1]), Float(p_zoom[2])]
  219. IF (Min(zoom) LE 0.0) THEN BEGIN
  220.    Print, 'Zoom factor must be > 0'
  221.    STOP
  222. ENDIF
  223.  
  224. zfac = 1.0
  225. IF (N_Elements(p_zfac) GT 0) THEN zfac = Float(p_zfac[0])
  226. IF (zfac LT 0.0) THEN BEGIN
  227.    Print, 'Zfac must be >= 0'
  228.    STOP
  229. ENDIF
  230.  
  231. persp = 0.0
  232. IF (N_Elements(p_persp) GT 0) THEN persp = Float(p_persp[0])
  233. p_proj = 0B
  234. IF (persp GT 0.0) THEN p_proj = 1B
  235.  
  236. ; *** Start setting up view
  237.  
  238. !X.Style = 1
  239. !X.Range = [xmin, xmax]
  240. !X.Margin = [0, 0]
  241. !Y.Style = 1
  242. !Y.Range = [ymin, ymax]
  243. !Y.Margin = [0, 0]
  244. !Z.Style = 1
  245. !Z.Range = [zmin, zmax]
  246. !Z.Margin = [0, 0]
  247.  
  248. !X.S = [(-xmin), 1.0] / (xmax - xmin)
  249. !Y.S = [(-ymin), 1.0] / (ymax - ymin)
  250. !Z.S = [(-zmin), 1.0] / (zmax - zmin)
  251.  
  252. !P.Position = [0.0, 0.0, 1.0, 1.0]
  253. !P.Clip = [0, 0, (winx-1), (winy-1), 0, 0]
  254. !P.Region = [0.0, 0.0, 1.0, 1.0]
  255. !P.T3D = 1
  256.  
  257. xfac = 1.0
  258. yfac = 1.0
  259. IF (winx GT winy) THEN xfac = winy / winx
  260. IF (winy GT winx) THEN yfac = winx / winy
  261.  
  262. xrange = xmax - xmin
  263. yrange = ymax - ymin
  264. zrange = (zmax - zmin) * zfac
  265. max_range = xrange > yrange > zrange
  266. xyz_fac = [xrange, yrange, zrange] / max_range
  267.  
  268. T3d, /Reset
  269. T3d, Translate=[(-0.5), (-0.5), (-0.5)]
  270. T3d, Scale=zoom
  271. T3d, Scale=xyz_fac
  272. T3d, Rotate=[0.0, 0.0, az]
  273. T3d, Rotate=[0.0, ay, 0.0]
  274. T3d, Rotate=[ax, 0.0, 0.0]
  275. IF (p_proj) THEN T3d, Perspective=persp
  276. T3d, Scale=[xfac, yfac, 1.0]
  277. T3d, Translate=[(0.5), (0.5), (0.5)]
  278.  
  279. RETURN
  280. END
  281.